package de.tabuma.sso.usergroup;

import java.lang.reflect.*;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.google.appengine.api.users.*;

import de.tabuma.lib.*;
import de.tabuma.lib.contentmanagement.entity.*;
import de.tabuma.sso.user.*;
import de.tabuma.sso.group.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 */
public class UserGroupController extends tabumacontroller {

    private static final Logger log = Logger.getLogger(UserGroupController.class.getName());

    public UserGroupController(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        try {
            String id = request.getParameter(de.tabuma.sso.constants.GROUP_PARAM+de.tabuma.lib.contentmanagement.constants.id);
            int intId = Integer.valueOf(id).intValue();

            Group group = GroupFacade.get(intId);
            String entityName = "User";
            String name = "user";

            String userGroupManagement = "Management of users for group {1}";
            String backToGroup = "back to group overview";

            out.println(view.getContentHeader(userGroupManagement.replaceAll("\\{1\\}", group.getName()+"("+group.getId()+")")));

            Hashtable newHash = (Hashtable) getParameter().clone();
            newHash.put(de.tabuma.sso.constants.GROUP_PARAM+de.tabuma.lib.contentmanagement.constants.id, ""+group.getId());
            newHash.put(de.tabuma.sso.constants.GROUP_USER_SIGN, "true");

            /*-----------------------------------*/

            Content content = new Content();

            Vector<Field> editableFields = new Vector<Field>();
            editableFields.add(de.tabuma.sso.usergroup.UserGroup.class.getDeclaredField("userId"));
            content.FormContent.setEditableFields(editableFields);

            Vector<Integer> editableTypes = new Vector<Integer>();
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_LIST));
            content.FormContent.setEditableFieldTypes(editableTypes);

            content.FormContent.setEntityClass(de.tabuma.sso.usergroup.UserGroup.class);

            content.FormContent.setEntityName("user");

            content.FormContent.setFacadeObject(new UserGroupFacade());

            UserService userService = UserServiceFactory.getUserService();
            com.google.appengine.api.users.User user = userService.getCurrentUser();
            content.ListContent.setListParameterValue(id);

            content.GeneralContent.setParamName(de.tabuma.sso.constants.USER_GROUP_PARAM);

            Vector<Field> listableFields = new Vector<Field>();
            listableFields.add(de.tabuma.sso.usergroup.UserGroup.class.getDeclaredField("userId"));
            content.ListContent.setListableFields(listableFields);

            Vector<Field> staticFields = new Vector<Field>();
            staticFields.add(de.tabuma.sso.usergroup.UserGroup.class.getDeclaredField("groupId"));
            content.FormContent.StaticContent.setStaticFields(staticFields);

            Vector<Integer> staticFieldsType = new Vector<Integer>();
            staticFieldsType.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_INTEGER));
            content.FormContent.StaticContent.setStaticFieldsTyp(staticFieldsType);

            Vector<String> staticFieldsValue = new Vector<String>();
            staticFieldsValue.add(id);
            content.FormContent.StaticContent.setStaticFieldsValue(staticFieldsValue);

            content.GeneralContent.setServlet(this.getServlet());

            content.GeneralContent.setParameter(newHash);

            Hashtable<Field, String> displayFields = new Hashtable<Field, String>();
            displayFields.put(de.tabuma.sso.usergroup.UserGroup.class.getDeclaredField("userId"), name);
            content.FormContent.setDisplayedName(displayFields);

            Hashtable<Field, List> listParameterContent = new Hashtable<Field, List>();
            List<de.tabuma.sso.user.User> userList = UserFacade.getList1(user.getEmail());
            listParameterContent.put(de.tabuma.sso.usergroup.UserGroup.class.getDeclaredField("userId"), userList);
            content.ListContent.setListParameterContent(listParameterContent);

            Hashtable<Field,List<Field>> listParameterContentDisplayedFields = new Hashtable<Field, List<Field>>();
            List<Field> displayFieldList = new ArrayList<Field>();
            displayFieldList.add(de.tabuma.sso.user.User.class.getDeclaredField("name"));
            listParameterContentDisplayedFields.put(de.tabuma.sso.usergroup.UserGroup.class.getDeclaredField("userId"), displayFieldList);
            content.ListContent.setListParameterDisplayedField(listParameterContentDisplayedFields);

            content.GeneralContent.setUser(user.getEmail());

            out.println(content.navigate(request));

            out.println("<div class=\"contentContent\">");
            out.println("<form action=\""+this.getServlet()+"\">");
            out.println(view.getHiddenAsAttribute(this.getParameter()));
            out.println("<input type=\"submit\" value=\""+backToGroup+"\" class=\"button\"/>");
            out.println("</form>");
            out.println("</div>");
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
    }
}